package com.example.module_mine.login.repository

import com.example.lib_net.api.Api
import com.example.lib_net.constant.API
import com.example.lib_net.model.api.BaseModel
import com.example.lib_net.model.api.LoginAuthModel
import com.example.module_mine.login.model.LoginPostModel
import com.example.module_mine.login.model.ResetPostModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn

import okhttp3.RequestBody
import retrofit2.http.Body
import retrofit2.http.Field
import retrofit2.http.POST
import javax.inject.Inject

/**
 * @author yaotianxue
 * @description
 * @date 2025/2/5
 */
class LoginRepository @Inject constructor() {
    @Inject
    lateinit var api: Api


    /**
     * 登陆发送验证码接口
     */
    fun sendLoginCode( phone:String) = flow {
        emit(api.sendLoginCode(phone))
    }.flowOn(Dispatchers.IO)

    /**
     * 验证码登陆校验
     */
    fun loginAuth(password:String,userName:String) = flow {
        val body = LoginPostModel(grantType = "sms",password = password,userName = userName)
        emit(api.loginAuth(body.toRequestBody()))
    }.flowOn(Dispatchers.IO)

    /**
     * 忘记密码发送验证码接口
     */
    fun sendForgetPwdCode(phone:String) = flow {
        emit(api.sendForgetPwdCode(phone))
    }.flowOn(Dispatchers.IO)

    /**
     * 忘记密码验证码验证接口
     */
     fun resetPwdAuth(smsCode:String,userName:String)= flow {
         val body = ResetPostModel( smsCode = smsCode,userName = userName)
         emit(api.resetPwdAuth(body.toRequestBody()))
    }.flowOn(Dispatchers.IO)
    /**
     * 绑定密码接口
     */
    fun resetPwd(smsCode:String,userName:String,password:String,newPassword:String) = flow {
        val body = ResetPostModel( smsCode = smsCode,userName = userName, password = password, newPassword = newPassword)
        emit(api.resetPwd(body.toRequestBody()))
    }.flowOn(Dispatchers.IO)

    /**
     * 账号密码登陆
     */
    fun login(password:String,userName:String) = flow {
        val body = LoginPostModel(grantType = "password",password = password,userName = userName)
        emit(api.login(body.toRequestBody()))
    }.flowOn(Dispatchers.IO)
}